package com.ioffice.export.excel.engine.data.excel.impl.tree;

import java.util.List;

import com.ioffice.exception.IOfficeException;
import com.ioffice.export.excel.data.ExcelSheetDataInfo;
import com.ioffice.export.excel.engine.data.excel.impl.AbstractEngineDataSheetCompent;
import com.ioffice.export.excel.sheet.cell.SheetCell;
import com.ioffice.export.excel.sheet.tree.SheetLeftHeader;
import com.ioffice.export.excel.type.ExcelSheetTreeType;
import com.ioffice.export.excel.type.ExcelSheetType;
import static com.ioffice.export.excel.engine.data.excel.EngineDataSheetCell.getSheetCell;
import static com.ioffice.export.excel.engine.data.excel.EngineDataSheetCell.addChidrenSheetCell;


public class EngineDataSheetTreeLeftHeader extends AbstractEngineDataSheetCompent{

	private  int deepCount=0;
	private  int leafCount=0;
	private int leftLeafChildCount = 0;
	private SheetCell parentCell=null;
	
	private ExcelSheetDataInfo parentData;
	private ExcelSheetDataInfo childData;
	
	
	public EngineDataSheetTreeLeftHeader() {
		compent=new SheetLeftHeader();
	}
	
	
	@Override
	public void data2SheetCompent(List<ExcelSheetDataInfo> datas,ExcelSheetType sheetType) throws IOfficeException {
		ExcelSheetTreeType treeType=(ExcelSheetTreeType)sheetType;
		leafCount=0;
		for(int i=0;i<datas.size();i++){
			parentData=datas.get(i);
			if(parentData.getChildLevel()==0){//得到root节点
				if(parentData.getParentId()==treeType.getLeftHeaderParentId()){//是否为上表头
					parentCell=getSheetCell(parentData);
					for(int j=i+1;j<datas.size();j++){
						childData=datas.get(j);
						boolean isHaveChild=true;
						if(parentData.getId()==childData.getParentId()){
							//是否为第一级节点并第一级节点的parentId为root节点的id
							if(childData.getChildLevel()==1){
								deepCount=0;
								isHaveChild=false;
								parentCell=getAllChildrenSheetCell(parentCell, childData.getParentId(), j, datas, 1);
							}
						}
						
						if(isHaveChild){//没有子节点
							setOnlyParentCell(parentCell, 0);
						}
					}
					((SheetLeftHeader)compent).setDeepCount(leftLeafChildCount);
					((SheetLeftHeader)compent).addChild(parentCell);
				}
			}
		}
		
	}

	public int getSheetLeftHeaderNodesLeaf(SheetLeftHeader left) throws IOfficeException{
		int childCount =left.getChildSize();
		leftLeafChildCount=0;
		for(int i=0;i<childCount;i++){
			getSheetCellNodesLeafCount(left.getSheetCell(i));
		}
		return leftLeafChildCount;
	}
	
	
	private void getSheetCellNodesLeafCount(SheetCell cell){
		int childCount = cell.getChildSize();
		if(childCount==0)
			leftLeafChildCount++;
		else{
			for(int i=0;i<childCount;i++){
				getSheetCellNodesLeafCount(cell.getSheetCell(i));
			}
		}
	}
	
	private void setOnlyParentCell(SheetCell Cell,int leftDeepCount) {
		leftDeepCount++;
		if(leftDeepCount>leftLeafChildCount){
			leftLeafChildCount=leftDeepCount;
		}
	}


	private SheetCell getAllChildrenSheetCell(SheetCell parentCell,int parentId,int stemp,List<ExcelSheetDataInfo> list,int leftDeepCount){
		ExcelSheetDataInfo info=list.get(stemp);
		leftDeepCount++;
		SheetCell cell=null;
		if(info.getParentId()==parentId){
			cell=getSheetCell(info);
			for(int i=stemp+1;i<list.size();i++){
				cell=getAllChildrenSheetCell(cell,info.getId(),i,list,leftDeepCount);
			}
			if(leftDeepCount>deepCount){
				deepCount=leftDeepCount;
			}
			if(deepCount>leftLeafChildCount){
				leftLeafChildCount=deepCount;
			}
			
			addChidrenSheetCell(parentCell, cell);
		}
		return parentCell;
	}
}
